package xyz.xiezc.spider.control;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.setting.Setting;
import lombok.extern.slf4j.Slf4j;
import xyz.xiezc.ioc.annotation.Component;
import xyz.xiezc.ioc.annotation.Inject;
import xyz.xiezc.spider.common.XBean;
import xyz.xiezc.spider.common.XUtil;
import xyz.xiezc.spider.statistic.Statistic;
import xyz.xiezc.xhttp.HttpServer;

import java.sql.SQLException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;

/**
 * 调度主类,
 *
 * @author wb-xzc291800
 * @date 2019/04/02 17:59
 */
@Slf4j
@Component
public class ScheduleImpl implements Schedule {

    @Inject
    HttpServer httpServer;

    @Inject
    Setting setting;

    @Inject
    Statistic statistic;

    ExecutorService singleExecutor = ThreadUtil.newSingleExecutor();

    @Inject
    ScheduleCore scheduleCore;


    @Override
    public Schedule addCommonHeader(String key, String value) {
        statistic.addCommonHeader(key, value);
        return this;
    }

    @Override
    public Schedule addSeedUrls(String... urls) {
        for (String url : urls) {
            statistic.addSeedUrl(url);
        }
        return this;
    }

    /**
     * 开始爬取
     */
    @Override
    public Schedule startHttp(int port, Executor executor) {
        httpServer.startHttpServer(port, executor);
        return this;
    }

    /**
     * 开始爬虫程序
     */
    @Override
    public Schedule startSpider() {
        singleExecutor.execute(() -> {
            setting.set("spider.stop", "false");
            statistic.recover();
            statistic.dealSeedUrls();
            while (true) {
                XUtil.foreach(setting.getInt("once.deal.num"), scheduleCore::download);
                XUtil.foreach(setting.getInt("once.deal.num"), scheduleCore::parse);
                XUtil.foreach(setting.getInt("once.deal.num"), scheduleCore::business);
                XUtil.foreach(setting.getInt("once.deal.num"), () -> {
                    XBean repeat = scheduleCore.repeat();
                    //统计一个网页
                    statistic.checkStop(repeat);
                });
                if (setting.getBool("spider.stop")) {
                    log.info("停止爬虫........................");
                    try {
                        scheduleCore.dealSeeAndLike();
                        scheduleCore.dealFirstImg();
                    } catch (SQLException e) {
                        log.error("", e);
                    }
                    break;
                }
            }


        });
        return this;
    }

}
